// audioMixin.js
import { useAudioStore } from "@/store/audio";
import { useSongListStore } from "@/store/songList";

export const audioMixin = {
  methods: {
    toPlay(id, url, pic, name, singerId, lyric, index) {
      const audioStore = useAudioStore();
      audioStore.id = id;
      audioStore.url = url;
      audioStore.pic = pic
      audioStore.title = name
      audioStore.singerId = singerId
      audioStore.listIndex = index
      audioStore.lyric =lyric
    },
    toPlayList() {
      const audioStore = useAudioStore();
      const songListStore = useSongListStore()
      audioStore.listOfSong = songListStore.songs
      audioStore.id = audioStore.listOfSong[0].id;
      audioStore.url = audioStore.listOfSong[0].url;
      audioStore.pic = audioStore.listOfSong[0].pic
      audioStore.title = audioStore.listOfSong[0].name
      audioStore.singerId = audioStore.listOfSong[0].singerId
      audioStore.lyric = audioStore.listOfSong[0].lyric
    },
    parseLyric(text) {
      let lines = text.split("\n")
      let pattern = /\[\d{2}:\d{2}.(\d{2}|\d{3})\]/g
      let result = []
      if (!(/\[.+\]/.test(text))) {
        return [[0, text]]
      }
      while (!pattern.test(lines[0])) {
        lines = lines.slice(1)
      }
      for (let item of lines) {
        let time = item.match(pattern)
        let value = item.replace(pattern, '')
        for (let item1 of time) {
          let t = item1.slice(1, -1).split(":")
          if (value != '') {
            result.push(parseInt(t[0], 10) * 60 + parseFloat(t[1]), value)
          }
        }
      }
      result.sort(function(a,b){
        return a[0] - b[0]
      })
      return result
    }
  }
};
